package com.fw.ui.view.settings.usercontrol.parentmerchant.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.bean.ErrorBean;
import com.fw.service.merchantprofile.bean.MerchantProfileBean;
import com.fw.service.parentmerchant.bean.ParentMerchantBean;
import com.fw.ui.ApplicationUI;
import com.fw.ui.bao.MasterDataBAO;
import com.fw.ui.bao.MerchantProfileBAO;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bao.ParentMerchantBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.bean.parentmerchant.SearchMerchantProfileTableItemBean;
import com.fw.ui.util.SessionInfoUtils;
import com.fw.ui.util.UICommonUtils;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.event.AbstractButtonClickListener;
import com.fw.ui.view.settings.usercontrol.parentmerchant.ConfirmParentMerchantLayout;
import com.fw.ui.view.settings.usercontrol.parentmerchant.EditParentMerchantView;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button.ClickEvent;

@Component
@Scope(SCOPE_PROTOTYPE)
public class EditParentMerchantSaveButtonClickListener extends AbstractButtonClickListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;
    @Autowired
    private MasterDataBAO masterDataBAO;
    @Autowired
    private ParentMerchantBAO parentMerchantBAO;
    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private UICommonUtils uiCommonUtils;
    @Autowired
    private SessionInfoUtils sessionInfoUtils;
    @Autowired
    private MerchantProfileBAO merchantProfileBAO;
    @Autowired
    private PropertyBAO propertyBAO;
    @Autowired
    private ConfirmParentMerchantLayout confirmParentMerchantLayout;

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public Map<String, Object> click(ClickEvent event) throws Exception {
	EditParentMerchantView editParentMerchantView = (EditParentMerchantView) this.getBasedComponent();
	Map<String, Object> result = new HashMap<String, Object>();

	String parentName = (String) editParentMerchantView.getTxtParentMerchantName().getValue();
	String parentBusniessAddress = (String) editParentMerchantView.getTxtBusinessAddress().getValue();
	String parentStatus = (String) editParentMerchantView.getCbbParentStatus().getValue();
	String contactPersonTitle = (String) editParentMerchantView.getCbbTitle().getValue();
	String contactPersonFirstName = (String) editParentMerchantView.getTxtFirstName().getValue();
	String contactPersonLastName = (String) editParentMerchantView.getTxtLastName().getValue();
	String contactPersonTelephoneNumber = (String) editParentMerchantView.getTxtTelephoneNumber().getValue();
	String contactPersonEmailAddress = (String) editParentMerchantView.getTxtEmailAddress().getValue();

	ParentMerchantBean parentMerchantBean = editParentMerchantView.getParentMerchantBean();
	parentMerchantBean.setParentName(parentName);
	parentMerchantBean.setParentBusniessAddress(parentBusniessAddress);
	parentMerchantBean.setParentStatus(parentStatus);
	parentMerchantBean.setContactPersonTitle(contactPersonTitle);
	parentMerchantBean.setContactPersonFirstName(contactPersonFirstName);
	parentMerchantBean.setContactPersonLastName(contactPersonLastName);
	parentMerchantBean.setContactPersonTelephoneNumber(contactPersonTelephoneNumber);
	parentMerchantBean.setContactPersonEmailAddress(contactPersonEmailAddress);

	Map<Long, BeanItem> selectedBeanItemMap = editParentMerchantView.getSelectedBeanItemMap();
	Set<MerchantProfileBean> merchantProfileBeans = new HashSet<MerchantProfileBean>();
	for (BeanItem beanItem : selectedBeanItemMap.values()) {
	    SearchMerchantProfileTableItemBean searchMerchantProfileTableItemBean = (SearchMerchantProfileTableItemBean) beanItem.getBean();
	    MerchantProfileBean merchantProfileBean = new MerchantProfileBean();
	    merchantProfileBean.setId(searchMerchantProfileTableItemBean.getId());
	    merchantProfileBeans.add(merchantProfileBean);
	}
	parentMerchantBean.setMerchantProfileBeans(merchantProfileBeans);

	ApplicationUI applicationUI = (ApplicationUI) editParentMerchantView.getUI();
	sessionInfoUtils.setSessionInfoDetails(parentMerchantBean, applicationUI.getAuthenticationBean(), applicationUI.getLocale());

	Map<String, Serializable> mapResult = parentMerchantBAO.updateParentMerchant(parentMerchantBean);

	if (mapResult != null) {
	    List<ErrorBean> errorBeans = (List<ErrorBean>) mapResult.get(List.class.getName() + "." + ErrorBean.class.getName());
	    ParentMerchantBean resultParentMerchantBean = (ParentMerchantBean) mapResult.get(ParentMerchantBean.class.getName());
	    if (errorBeans != null && !errorBeans.isEmpty()) {
		result.put(MessageBAO.MESSAGE_WARNING_TYPE, errorBeans);
	    } else {
		if (resultParentMerchantBean != null && resultParentMerchantBean.getId() != null) {

		    uiCommonUtils.setValueUIComponent(editParentMerchantView.getTxtParentMerchantID(),
			    resultParentMerchantBean.getParentId());
		    uiCommonUtils.setValueUIComponent(editParentMerchantView.getTxtParentMerchantName(),
			    resultParentMerchantBean.getParentName());
		    uiCommonUtils.setValueUIComponent(editParentMerchantView.getTxtBusinessAddress(),
			    resultParentMerchantBean.getParentBusniessAddress());
		    uiCommonUtils.setValueUIComponent(editParentMerchantView.getCbbParentStatus(),
			    resultParentMerchantBean.getParentStatus());
		    uiCommonUtils.setValueUIComponent(editParentMerchantView.getCbbTitle(),
			    resultParentMerchantBean.getContactPersonTitle());
		    uiCommonUtils.setValueUIComponent(editParentMerchantView.getTxtFirstName(),
			    resultParentMerchantBean.getContactPersonFirstName());
		    uiCommonUtils.setValueUIComponent(editParentMerchantView.getTxtLastName(),
			    resultParentMerchantBean.getContactPersonLastName());
		    uiCommonUtils.setValueUIComponent(editParentMerchantView.getTxtTelephoneNumber(),
			    resultParentMerchantBean.getContactPersonTelephoneNumber());
		    uiCommonUtils.setValueUIComponent(editParentMerchantView.getTxtEmailAddress(),
			    resultParentMerchantBean.getContactPersonEmailAddress());

		    uiCommonUtils.clearAllUIComponentNoInputFieldWithPrefix(editParentMerchantView);

		    String closeStatus = propertyBAO
			    .getPropertyValue("com.fw.ui.view.settings.usercontrol.parentmerchant.EditMerchantProfileView.status.item.closed");
		    if (closeStatus.equals(resultParentMerchantBean.getParentStatus())) {
			setAutoEnabled(false);
			List<com.vaadin.ui.Component> componentsEnable = new ArrayList<com.vaadin.ui.Component>();
			componentsEnable.add(editParentMerchantView.getBtnCancel());
			uiCommonUtils.setEnableAllUIComponent(editParentMerchantView, componentsEnable, false);
		    }

		    result.put(MessageBAO.MESSAGE_INFO_TYPE,
			    "com.fw.ui.view.settings.usercontrol.parentmerchant.EditParentMerchantView.message.updated");
		} else {
		    result.put(MessageBAO.MESSAGE_WARNING_TYPE,
			    "com.fw.ui.view.settings.usercontrol.parentmerchant.EditParentMerchantView.message.failed");
		}
	    }
	} else {
	    result.put(MessageBAO.MESSAGE_WARNING_TYPE, "message.error.common");
	}

	return result;
    }

    @Override
    public boolean validate(ClickEvent event) {
	EditParentMerchantView editParentMerchantView = (EditParentMerchantView) this.getBasedComponent();
	try {

	    uiCommonUtils.clearAllUIComponentNoInputFieldWithPrefix(editParentMerchantView);

	    boolean parentMerchantIDValid = editParentMerchantView.getTxtParentMerchantID().isValid();
	    boolean parentMerchantNameValid = editParentMerchantView.getTxtParentMerchantName().isValid();
	    boolean businessAddressValid = editParentMerchantView.getTxtBusinessAddress().isValid();
	    boolean parentStatusValid = editParentMerchantView.getCbbParentStatus().isValid();
	    boolean titleValid = editParentMerchantView.getCbbTitle().isValid();
	    boolean firstNameValid = editParentMerchantView.getTxtFirstName().isValid();
	    boolean lastNameValid = editParentMerchantView.getTxtLastName().isValid();
	    boolean telephoneNumberValid = editParentMerchantView.getTxtTelephoneNumber().isValid();
	    boolean emailAddressValid = editParentMerchantView.getTxtEmailAddress().isValid();

	    if (parentMerchantIDValid && parentMerchantNameValid && businessAddressValid && parentStatusValid && titleValid
		    && firstNameValid && lastNameValid && telephoneNumberValid && emailAddressValid) {
		return true;
	    } else {
		if (!parentMerchantIDValid) {
		    editParentMerchantView.getTxtParentMerchantID().addStyleName("v-textfield-error");
		    if (editParentMerchantView.getTxtParentMerchantID().getErrorMessage() != null) {
			editParentMerchantView.getLblParentMerchantIDValidateMessage().setValue(
				editParentMerchantView.getTxtParentMerchantID().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editParentMerchantView.getLblParentMerchantIDValidateMessage().setVisible(true);
		}
		if (!parentMerchantNameValid) {
		    editParentMerchantView.getTxtParentMerchantName().addStyleName("v-textfield-error");
		    if (editParentMerchantView.getTxtParentMerchantName().getErrorMessage() != null) {
			editParentMerchantView.getLblParentMerchantNameValidateMessage().setValue(
				editParentMerchantView.getTxtParentMerchantName().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editParentMerchantView.getLblParentMerchantNameValidateMessage().setVisible(true);
		}
		if (!businessAddressValid) {
		    editParentMerchantView.getTxtBusinessAddress().addStyleName("v-textfield-error");
		    if (editParentMerchantView.getTxtBusinessAddress().getErrorMessage() != null) {
			editParentMerchantView.getLblBusinessAddressValidateMessage().setValue(
				editParentMerchantView.getTxtBusinessAddress().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editParentMerchantView.getLblBusinessAddressValidateMessage().setVisible(true);
		}
		if (!parentStatusValid) {
		    editParentMerchantView.getCbbParentStatus().addStyleName("v-textfield-error");
		    if (editParentMerchantView.getCbbParentStatus().getErrorMessage() != null) {
			editParentMerchantView.getLblParentStatusValidateMessage().setValue(
				editParentMerchantView.getCbbParentStatus().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editParentMerchantView.getLblParentStatusValidateMessage().setVisible(true);
		}
		if (!titleValid) {
		    editParentMerchantView.getCbbTitle().addStyleName("v-textfield-error");
		    if (editParentMerchantView.getCbbTitle().getErrorMessage() != null) {
			editParentMerchantView.getLblTitleValidateMessage().setValue(
				editParentMerchantView.getCbbTitle().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editParentMerchantView.getLblTitleValidateMessage().setVisible(true);
		}
		if (!firstNameValid) {
		    editParentMerchantView.getTxtFirstName().addStyleName("v-textfield-error");
		    if (editParentMerchantView.getTxtFirstName().getErrorMessage() != null) {
			editParentMerchantView.getLblFirstNameValidateMessage().setValue(
				editParentMerchantView.getTxtFirstName().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editParentMerchantView.getLblFirstNameValidateMessage().setVisible(true);
		}
		if (!lastNameValid) {
		    editParentMerchantView.getTxtLastName().addStyleName("v-textfield-error");
		    if (editParentMerchantView.getTxtLastName().getErrorMessage() != null) {
			editParentMerchantView.getLblLastNameValidateMessage().setValue(
				editParentMerchantView.getTxtLastName().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editParentMerchantView.getLblLastNameValidateMessage().setVisible(true);
		}
		if (!telephoneNumberValid) {
		    editParentMerchantView.getTxtTelephoneNumber().addStyleName("v-textfield-error");
		    if (editParentMerchantView.getTxtTelephoneNumber().getErrorMessage() != null) {
			editParentMerchantView.getLblTelephoneNumberValidateMessage().setValue(
				editParentMerchantView.getTxtTelephoneNumber().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editParentMerchantView.getLblTelephoneNumberValidateMessage().setVisible(true);
		}
		if (!emailAddressValid) {
		    editParentMerchantView.getTxtEmailAddress().addStyleName("v-textfield-error");
		    if (editParentMerchantView.getTxtEmailAddress().getErrorMessage() != null) {
			editParentMerchantView.getLblEmailAddressValidateMessage().setValue(
				editParentMerchantView.getTxtEmailAddress().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editParentMerchantView.getLblEmailAddressValidateMessage().setVisible(true);
		}
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    return false;
	}
	return false;
    }

}
